package com.beamfield.controller.api.mb;

import com.beamfield.common.CommonUtils;
import com.beamfield.controller.common.MbBaseController;
import com.beamfield.model.FileInfo;
import com.beamfield.service.manage.sys.FileService;
import net.coobird.thumbnailator.Thumbnails;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.Date;


/**
 * @author Zephyr
 * @Description:
 * @Date 2018/10/29
 */
@RequestMapping(value = "/mb_api")
@Controller
public class MbFileUploadController extends MbBaseController {

    private static final Logger log = LoggerFactory.getLogger(MbFileUploadController.class);

    @Resource
    private FileService fileService;

    @Value("${uploadFilePath}")
    private String uploadFilePath;

    @RequestMapping(value = "/upload",method = RequestMethod.POST)
    @ResponseBody
    @Transactional(readOnly = false, propagation = Propagation.SUPPORTS)
    public long saveFile(@RequestParam("file") MultipartFile file) {
        int fileid = 0;
        try {
            if (file != null) {
                String myFileName = file.getOriginalFilename();
                if (!CommonUtils.isEmptyString(myFileName)) {
                    String savePath = CommonUtils.getTimeFormat(new Date(), "yyyyMMdd") + File.separator;
                    String path = uploadFilePath+ savePath;
                    // 定义上传路径
                    CommonUtils.checkPath(path);
                    int index = myFileName.lastIndexOf(".");
                    String extension = "";
                    if (index > 0) {
                        extension = myFileName.substring(index, myFileName.length());
                    }
//					String filename = CommonUtils.getTimeFormat(new Date(), "yyyyMMddhhmmssSSS") + "_"
//							+ (int) (Math.random() * 100) + extension;
                    OutputStream out = new FileOutputStream(path + myFileName);
                    byte[] b = null;
                    if(file.getSize()>200*1024){
                        if(".jpg".equals(extension.toLowerCase())||".jpeg".equals(extension.toLowerCase())){
                            Thumbnails.of(file.getInputStream()).scale(0.9f).outputQuality(0.4).toOutputStream(out);
                        }
                        if(".png".equals(extension.toLowerCase())){
                            Thumbnails.of(file.getInputStream()).scale(0.6f).outputQuality(0.5).toOutputStream(out);
                        }
                        else{
                            b = file.getBytes();
                        }
                    }else{
                        b = file.getBytes();
                    }
                    if(b != null){
                        out.write(b);
                    }
                    out.flush();
                    out.close();

                    // 文件表留档
                    FileInfo fileInfo = new FileInfo();
                    fileInfo.setAdd_time(new Date());
                    fileInfo.setFile_path(savePath);
                    fileInfo.setOriginal_name(myFileName);
                    fileInfo.setFormer_name(myFileName);
                    fileInfo = fileService.saveFileInfo(fileInfo);
                    // fileid = fileInfo.getFileid();
                    return fileInfo.getFileid();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return fileid;
    }


}
